﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Text.RegularExpressions;
using Db8.Data;
using Db8.DataAccess;

namespace Db8.ViewModels
{
    public class ClaimViewModel
    {
        public virtual Claim Claim { get; set; }

        public string Filter { get; set; }

        public string AbstractHtml
        {
            get 
            {
                string html = HttpUtility.HtmlEncode(Claim.Abstract);
                html = html.Replace("\r\n", " <br>");
                html = html.Replace("\n", " <br>");
                html = html.Replace("\r", " <br>");
                html = html.Replace("()", "&bull;");
                html = html.Replace("  ", "&nbsp;&nbsp;");
                
                Regex re;
                
                re = new Regex(@"\*[^*]+\*");
                html = re.Replace(html, s => "<strong>" + s.Value.Substring(1, s.Value.Length - 2) + "</strong>");
                re = new Regex(@"_[^_]+_");
                html = re.Replace(html, s => "<em>" + s.Value.Substring(1, s.Value.Length - 2) + "</em>");


                return html;
            }
        
        } 
        public virtual string Keywords { get; set; }

        public static ClaimViewModel FromClaim(Claim claim)
        {
            if (claim == null)
                return null;
            return new ClaimViewModel()
            {
                Claim = claim,
                Keywords = claim.ClaimKeywords != null ? claim.ClaimKeywords.Select(ck => ck.Keyword.Value).DefaultIfEmpty("").Aggregate((a, b) => a + ", " + b) : ""
            };
        }


        public List<ClaimViewModel> CounterClaims { get; set; }
    }
}